﻿
Imports System.Data
Imports VSModel
Imports Cls_Common
Imports SecurityService
Partial Class Control_MSDS_Section16
    Inherits System.Web.UI.UserControl
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
#Region "Sub and Function "
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim script As ScriptManager = ScriptManager.GetCurrent(Me.Page)
            If Not script Is Nothing AndAlso script.IsInAsyncPostBack Then
                ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType, "jqueryEnable", "ajaxJquery()", True)
            Else
                Me.Page.ClientScript.RegisterStartupScript(Me.Page.GetType(), "jqueryEnable", String.Concat("Sys.Application.add_load(function(){", "ajaxJquery()", "});"), True)
            End If

            hidMSDSId.Value = Request("Id")
            ShowData()
        End If
    End Sub
    Protected Sub ShowData()
        Using data As New VSEntities
            Dim _lang As Integer = Session("LanguageId")
            Dim _Section16Id As Integer = (From q In data.MSDSSection16 Where q.MSDSId = hidMSDSId.Value Select q.Section16Id).FirstOrDefault
            If _Section16Id <> 0 Then
                Dim p As MSDSSection16 = (From q In data.MSDSSection16 Where q.Section16Id = _Section16Id Select q).FirstOrDefault
                If Not p Is Nothing Then
                    'txtMsdsid.Text = IIf(IsNothing(p.MSDSId) = True, "", p.MSDSId)
                    txtS1611.Text = IIf(IsNothing(p.S1611) = True, "", p.S1611)
                    txtS1621.Text = IIf(IsNothing(p.S1621) = True, "", p.S1621)
                    txtS1631.Text = IIf(IsNothing(p.S1631) = True, "", p.S1631)
                    txtS1641.Text = IIf(IsNothing(p.S1641) = True, "", p.S1641)
                    txtS1651.Text = IIf(IsNothing(p.S1651) = True, "", p.S1651)
                    txtS1661.Text = IIf(IsNothing(p.S1661) = True, "", p.S1661)
                    hidID.Value = p.Section16Id
                End If
            End If

            'Label
            Translate()
        End Using
    End Sub

    Protected Sub ResetControl()

        txtS1611.Text = ""
        txtS1621.Text = ""
        txtS1631.Text = ""
        txtS1641.Text = ""
        txtS1651.Text = ""
        txtS1661.Text = ""

    End Sub
#End Region
#Region "Event for control "
    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Using data As New VSEntities
            If hidID.Value <> 0 Then
                Dim p = (From q In data.MSDSSection16 Where q.Section16Id = hidID.Value Select q).FirstOrDefault
                p.MSDSId = hidMSDSId.Value
                p.S1611 = txtS1611.Text.Trim()
                p.S1621 = txtS1621.Text.Trim()
                p.S1631 = txtS1631.Text.Trim()
                p.S1641 = txtS1641.Text.Trim()
                p.S1651 = txtS1651.Text.Trim()
                p.S1661 = txtS1661.Text.Trim()
                p.Modified = Now
                p.Modifier = Session("UserName")
                p.LanguageId = Session("LanguageId")

                data.SaveChanges()
                Insert_App_Log("Insert  Msds Section16 :" & hidMSDSId.Value & "", Function_Name.MSDSOriginal, Audit_Type.Edit, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
                Excute_Javascript("Alertbox('Cập nhật dữ liệu thành công.')", Me.Page, True)

            Else

                Dim p As New VSModel.MSDSSection16
                Try
                    p.MSDSId = hidMSDSId.Value
                    p.S1611 = txtS1611.Text.Trim()
                    p.S1621 = txtS1621.Text.Trim()
                    p.S1631 = txtS1631.Text.Trim()
                    p.S1641 = txtS1641.Text.Trim()
                    p.S1651 = txtS1651.Text.Trim()
                    p.S1661 = txtS1661.Text.Trim()
                    p.Created = Now
                    p.Creator = Session("UserName")
                    p.LanguageId = Session("LanguageId")
                    data.MSDSSection16.AddObject(p)
                    data.SaveChanges()
                    Insert_App_Log("Insert  Msds Section16 :" & hidMSDSId.Value & "", Function_Name.MSDSOriginal, Audit_Type.Create, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
                    Excute_Javascript("Alertbox('Cập nhật dữ liệu thành công.')", Me.Page, True)
                Catch ex As Exception
                    log4net.Config.XmlConfigurator.Configure()
                    log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)

                End Try
            End If
        End Using


    End Sub



    Protected Sub Translate()
        Section16lblHeader.Text = getTranslate("Section16lblHeader", Session("LanguageId"), Section16lblHeader.Text)
        Section16ErrorNgayThangBienSoan.ErrorMessage = getTranslate("Section16ErrorNgayThangBienSoan", Session("LanguageId"), Section16ErrorNgayThangBienSoan.ErrorMessage)
        lblS1611.Text = getTranslate("lblS1611", Session("LanguageId"), lblS1611.Text)
        Session16lblNgayThangBienSoanPhieu.Text = getTranslate("Session16lblNgayThangBienSoanPhieu", Session("LanguageId"), Session16lblNgayThangBienSoanPhieu.Text)
        lblS1631.Text = getTranslate("lblS1631", Session("LanguageId"), lblS1631.Text)
        lblCompany.Text = getTranslate("lblCompany", Session("LanguageId"), lblCompany.Text)
        lblExpired.Text = getTranslate("lblExpired", Session("LanguageId"), lblExpired.Text)
        lblS1611.Text = getTranslate("lblS1611", Session("LanguageId"), lblS1611.Text)
        ltrRequired.Text = getTranslate("ltrRequired", Session("LanguageId"), ltrRequired.Text)
    End Sub

#End Region

    Protected Sub btnPrevious_Click(sender As Object, e As System.EventArgs) Handles btnPrevious.Click
        Response.Redirect("Section15.aspx?Id=" & hidMSDSId.Value)
    End Sub
End Class
